﻿using GpCommon.Helpers;
using GPSpeedView.GPHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace GPSpeedView.Models
{
    /// <summary>
    /// 盘中速涨股数据类
    /// </summary>
    public class MidFastTopGpEntity
    {
        public string Code { get; set; }
        public string Name { get; set; }
        /// <summary>
        /// 记录时间
        /// </summary>
        public DateTime RecordTime { get; set; }
        /// <summary>
        /// 记录时价格
        /// </summary>
        public float RecordPrice { get; set; }
        /// <summary>
        /// 记录时涨幅
        /// </summary>
        public float RecordMarkUp { get; set; }
        /// <summary>
        /// 收盘价格
        /// </summary>
        public float EndPrice { get; set; }
        /// <summary>
        /// 收盘涨幅
        /// </summary>
        public float EndMarkUp { get; set; }

        public static MidFastTopGpEntity Create(byte[] bytes)
        {
            if (bytes.Length != 20) { return null; }
            MidFastTopGpEntity ent = null;
            try
            {
                int timeInt = BitConverter.ToInt32(new byte[] { bytes[0], bytes[1], 0, 0 }, 0);
                int year = (int)(Math.Floor(timeInt / 2048.0) + 2004);
                int month = (int)Math.Floor(timeInt % 2048 / 100.0);
                int day = timeInt % 2048 % 100;

                int minuteInt = BitConverter.ToInt32(new byte[] { bytes[2], bytes[3], 0, 0 }, 0);
                int hour = minuteInt / 60;
                int minute = minuteInt % 60;

                string code = BitConverter.ToInt32(new byte[] { bytes[4], bytes[5], bytes[6], bytes[7] }, 0).ToString("D6");

                float recordPrice = BitConverter.ToSingle(new byte[] { bytes[8], bytes[9], bytes[10], bytes[11] }, 0);
                float endPrice = BitConverter.ToSingle(new byte[] { bytes[12], bytes[13], bytes[14], bytes[15] }, 0);
                float lastEndPrice = BitConverter.ToSingle(new byte[] { bytes[16], bytes[17], bytes[18], bytes[19] }, 0);

                ent = new MidFastTopGpEntity();
                ent.Code = code;
                ent.Name = GpLocalDataHelper.GetValidGps().Find(x => x.Item1 == code)?.Item2;
                ent.RecordTime = new DateTime(year, month, day, hour, minute, 0);
                ent.RecordPrice = recordPrice;
                ent.RecordMarkUp = (float)Math.Round((recordPrice - lastEndPrice) * 100.0/ lastEndPrice,2);
                ent.EndPrice = endPrice;
                ent.EndMarkUp = (float)Math.Round((endPrice - lastEndPrice) * 100.0 / lastEndPrice, 2); ;
            }
            catch (Exception ex)
            {
                return null;
            }

            return ent;
        }
    }
    public static class MidFastTopGpEntityUtils
    {
        public static byte[] GetBytes(this MidFastTopGpEntity ent)
        {
            List<byte> writeByte = new List<byte>();
            var time1Bytes = BitConverter.GetBytes(2048 * (ent.RecordTime.Year - 2004) + 100 * ent.RecordTime.Month + ent.RecordTime.Day);
            writeByte.Add(time1Bytes[0]);
            writeByte.Add(time1Bytes[1]);
            var time2Bytes = BitConverter.GetBytes(ent.RecordTime.Hour * 60 + ent.RecordTime.Minute);
            writeByte.Add(time2Bytes[0]);
            writeByte.Add(time2Bytes[1]);
            writeByte.AddRange(BitConverter.GetBytes(int.Parse(ent.Code)));
            writeByte.AddRange(BitConverter.GetBytes(ent.RecordPrice));
            writeByte.AddRange(BitConverter.GetBytes(ent.EndPrice));
            
            writeByte.AddRange(BitConverter.GetBytes((float)Math.Round(ent.RecordPrice / (ent.RecordMarkUp / 100.0 + 1), 2, MidpointRounding.AwayFromZero)));
            return writeByte.ToArray();
        }
    }


}
